<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Connect to the MySQL database
 * @return \mysqli The database object
 */
function connectToDatabase() {
    return new mysqli("localhost", "rmplanne_andrew", "Pi39RQ", "rmplanne_rmplanner");
}

/**
 * Get the meals for 28 days from the start date
 * @param DateTime $startDate 
 */
function getMeals() {
    $db = connectToDatabase();
    $results = $db->query("SELECT * FROM meals 
        JOIN mealtime_meals ON mealtime_meals.meal_id = meals.meal_id
        JOIN mealtimes ON mealtimes.mealtime_id = mealtime_meals.mealtime_id
        WHERE mealtimes.mealtime_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 27 DAY");
    $db->commit();
    
    header("Content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
    echo "<data>\n";
    echo "<count>$results->num_rows</count>\n";
       
    while ($row = $results->fetch_assoc()) {
        $id = $row["meal_id"];
        $name = $row["meal_title"];
        $description = $row["meal_title"];
        echo "<meal>\n";
        echo "<id>$id</id>\n";
        echo "<name>$name</name>\n";
        echo "<description>$description</description>\n";
        echo "</meal>\n";
    }
    echo "</data>\n";
    $db->close();
}

getMeals();
?>
